package com.jiandian.controller;

import com.jiandian.crudparams.dto.AddSignPrizeLogDto;
import com.jiandian.crudparams.dto.EditSignPrizeLogDto;
import com.jiandian.crudparams.qo.SignPrizeLogQo;
import com.jiandian.crudparams.swaggernotes.SignPrizeLogNotes;
import com.jiandian.crudparams.vo.OnlineSignPrizeLogVo;
import com.jiandian.entity.SignPrizeLog;
import com.jiandian.entity.SignUser;
import com.jiandian.page.PageInfo;
import com.jiandian.service.SignPrizeLogService;
import com.jiandian.service.SignUserService;
import com.jiandian.util.BaseController;
import com.jiandian.util.BaseResult;
import com.jiandian.util.BeanUtils;
import com.jiandian.util.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 抽奖日志
 *
 * @author houqi Fu
 * @date 2018/12/15
 */
@Api(value = "SignPrizeLog", description = "抽奖日志")
@RestController
@RequestMapping(value = Constants.CODE_VERSION + "/SignPrizeLog")
public class SignPrizeLogController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(SignPrizeLogController.class);

    @Autowired
    private SignPrizeLogService service;

    @Autowired
    private SignUserService signUserService;

    @ApiOperation(value = "添加", notes = SignPrizeLogNotes.ADD_NOTE)
    @ApiImplicitParam(name = "record", value = SignPrizeLogNotes.ADD_VALUE, required = true, dataType = "AddSignPrizeLogDto")
    @PostMapping("/")
    public BaseResult add(@RequestBody @Validated AddSignPrizeLogDto record, BindingResult result){
        logger.info("添加抽奖日志，参数：\n {}", ReflectionToStringBuilder.toString(record));
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            logger.error("添加抽奖日志参数错误:{}", fieldError.getDefaultMessage());
            return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, fieldError.getDefaultMessage());
        }
        SignPrizeLog bean = new SignPrizeLog();
        BeanUtils.copyProperties(record, bean);
        SignPrizeLog saveResult = service.add(bean);
        if (saveResult != null) {
            return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, saveResult);
        }
        return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    }

    @ApiOperation(value = "修改", notes = SignPrizeLogNotes.EDIT_NOTE)
    @ApiImplicitParam(name = "record", value = SignPrizeLogNotes.EDIT_VALUE, required = true, dataType = "OnlineSignPrizeLogVo")
    @PutMapping("/{id}")
    public BaseResult edit(@PathVariable("id") String id,@RequestBody @Validated EditSignPrizeLogDto record, BindingResult result){
        logger.info("修改抽奖日志，参数：\n {}", ReflectionToStringBuilder.toString(record));
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            logger.error("修改抽奖日志参数错误:{}", fieldError.getDefaultMessage());
            return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, fieldError.getDefaultMessage());
        }
        SignPrizeLog bean = new SignPrizeLog();
        BeanUtils.copyProperties(record, bean);
        bean.setId(Long.valueOf(id));
        SignPrizeLog updateResult = service.update(bean);
        if (updateResult != null) {
            return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, updateResult);
        }
        return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    }

    @ApiOperation(value = "列表", notes = SignPrizeLogNotes.LIST_NOTE)
    @ApiImplicitParam(name = "pageInfo", value = SignPrizeLogNotes.LIST_VALUE, required = true, dataType = "PageInfo")
    @GetMapping("/")
    public BaseResult list(SignPrizeLogQo whereEntity, PageInfo pageInfo){
        logger.info("查询抽奖日志，参数：\n {}", ReflectionToStringBuilder.toString(pageInfo));
        PageInfo pageResult = service.getPage(whereEntity, pageInfo);

        return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200,pageResult);
    }
    
    @ApiOperation(value = "详情", notes = "抽奖日志详情")
    @GetMapping("/{id}")
    public BaseResult detail(@PathVariable("id") String id){
        logger.info("查询抽奖日志，参数：\n id = {}", id);
        if (StringUtils.isBlank(id)) {
            return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "参数uuid不存在");
        }
        SignPrizeLog record = service.findById(Long.valueOf(id));
        if (record != null) {
            return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, record);
        }
        return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "记录不存在");
    }

    @ApiOperation(value = "删除", notes = "根据抽奖日志id删除")
    @DeleteMapping("/{id}")
    public BaseResult delete(@PathVariable("id") String id){
        logger.info("删除抽奖日志，参数：\n id = {}", id);
        if (StringUtils.isBlank(id)) {
            return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "参数id不存在");
        }
        int deleteResult = service.delete(Long.valueOf(id));
        if (deleteResult > 0) {
            return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, "删除成功");
        }
        return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    }

    @ApiOperation(value = "批量删除", notes = "根据抽奖日志id list批量删除")
    @PostMapping("batchDelete")
    public BaseResult batchDelete(@RequestBody List<Long> ids){
        logger.info("批量删除抽奖日志，参数：\n {}", ReflectionToStringBuilder.toString(ids, ToStringStyle.MULTI_LINE_STYLE));
        if (CollectionUtils.isEmpty(ids)) {
            return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "参数ids不存在");
        }
        int deleteResult = service.batchDeleteById(ids);
        if (deleteResult > 0) {
            return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, "删除成功");
        }
        return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    }

    @ApiOperation(value = "消费奖品")
    @PostMapping("/consume")
    public void consume(@RequestParam Long id){

    }

    @ApiOperation(value = "补签")
    @GetMapping("/supple")
    public BaseResult supple(@RequestParam Integer userId){
        SignUser byId = signUserService.findById(userId);
        boolean supple = signUserService.supple(byId);
        if (supple ) {
            return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, "补签成功");
        }
        return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "补签失败");

    }

    @ApiOperation(value = "根据上次返回的最后一个id，查找新增数据")
    @GetMapping("/selectByMinId")
    public BaseResult selectByMinId(@RequestParam Long minId){
        List<OnlineSignPrizeLogVo> onlineSignPrizeLogVos = service.selectByMinId(minId);
        return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, onlineSignPrizeLogVos);
    }
}
